package in.smartride.dataInterface;

import in.smartride.dataInterface.userdata.UserPreferences;
import in.smartride.dataInterface.userdata.UserProfile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.jdo.PersistenceManager;

public class DataHandler {
	PersistenceManager pm = PMF.get().getPersistenceManager();
	private int requestid, offerid;

	public DataHandler() {
		requestid = 0;
		offerid = 0;
		// LandMark lm = new LandMark("IIIT",10,20);
		// addLandMark(lm);
	}

	public void alm() throws IOException {
		String lm = "IIIT bus stop		     ,17.44501,78.35312~indra nagar bus stop	     ,17.44134,78.36001~gachibowli bus stop          ,17.43895,78.36376~anjaya nagar  bus stop       ,17.45197,78.36375~kothaguda bus stop           ,17.45892,78.36721~raidurgam bus stop           ,17.42282,78.38142~shaikpet bus stop            ,17.40621,78.40318~Tolichowki Bus Stop          ,17.39856,78.41531~Salarjung Colony Bus Stop    ,17.39666,78.42212~nanalnagar Bus Stop          ,17.39596,78.42708~Rethibowli Bus Stop          ,17.39505,78.43478~Mehdipatnam City Bus Station ,17.39474,78.44055~Mehdipatnam  Bus Depot       ,17.39216,78.44100~Milan Bus Stop               ,17.39188,78.44762~Mallepally Bus Stop          ,17.38917,78.45440~Sarojini Bus Stop            ,17.39879,78.44485~NMDC Bus Stop                ,17.39905,78.44959~Masabtank Bus Stop           ,17.40434,78.45176~Mahavir Bus Stop             ,17.40285,78.45798~Lakdikapul Bus Stop          ,17.40527,78.45956~HCU Bus Depot                ,17.46706,78.32999~Gulmohar Park Bus Stop       ,17.47807,78.32999~Lingampally MMTS station     ,17.48317,78.31674~Kondapur Bus Stop            ,17.46426,78.36704~HITEC City Bus Stop          ,17.45081,78.38136~Madhapur Bus Stop            ,17.44378,78.38762~Jubilee Bus Stop             ,17.42682,78.41556~nandi nagar Bus Stop         ,17.41992,78.43092";
		String lms[] = lm.split("~");
		for (String landMark : lms) {
			String values[] = landMark.split(",");
			Long lat = Long.valueOf("10");
			Long longitude = Long.valueOf("20");
			LandMark landM = new LandMark(values[0], lat, longitude);
			pm.makePersistent(landM);
		}
	}

	public boolean addLandMark(LandMark landMark) {
		try {
			pm.makePersistent(landMark);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	@SuppressWarnings("unchecked")
	public LandMark getLandmark(String landmarkname) {
		String query = "select from " + LandMark.class.getName();
		List<LandMark> lm = (List<LandMark>) pm.newQuery(query).execute();
		System.out.println("sm");
		return lm.get(0);
	}

	// store ride request in database
	public void addRideRequest(String from, String to, Date date, String uId) {
		System.out.println("In addRideRequest " + uId);
		RideRequest request = new RideRequest(from, to, date, uId);
		try {
			pm.makePersistent(request);
		} finally {
			// pm.close();
		}
	}

	// store ride offer in database
	public void addRideOffer(String from, String to, Date date, String uId) {
		System.out.println("In addRideoffer " + uId);
		RideOffer offer = new RideOffer(from, to, date, uId);
		try {
			pm.makePersistent(offer);
		} finally {
			// pm.close();
		}
	}

	// retrieve ride request in database
	@SuppressWarnings("unchecked")
	public List<RideRequest> retrieveRideRequest(String from, String to,
			Date date) {
		System.out.println("In retrieve Ride Request " + from + " " + to + " "
				+ date.toString());
		javax.jdo.Query query = pm.newQuery(RideRequest.class);
		query
				.setFilter("from == fromLocation && to == toLocation && date == onDate");
		query
				.declareParameters("java.lang.String fromLocation, java.lang.String toLocation, java.util.Date onDate");

		try {
			List<RideRequest> results = (List<RideRequest>) query.execute(from,
					to, date);
			return results;
		} catch (Exception e) {
			e.printStackTrace();
			query.closeAll();
		}
		return null;
	}

	// retrieve ride offer in database
	@SuppressWarnings("unchecked")
	public List<RideOffer> retrieveRideOffer(String from, String to, Date date) {

		System.out.println("In retrieve Ride Offer " + from + " " + to + " "
				+ date.toString());
		javax.jdo.Query query = pm.newQuery(RideOffer.class);
		query
				.setFilter("from == fromLocation && to == toLocation && date == onDate");
		query
				.declareParameters("java.lang.String fromLocation, java.lang.String toLocation, java.util.Date onDate");

		try {
			List<RideOffer> results = (List<RideOffer>) query.execute(from, to,
					date);
			return results;
		} catch (Exception e) {
			e.printStackTrace();
			query.closeAll();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<LandMark> getAllLandmarks() {
		String query = "select from " + LandMark.class.getName();
		List<LandMark> lm = (List<LandMark>) pm.newQuery(query).execute();
		System.out.println(lm);
		return lm;
	}

	public void updateUserProfile(String uId, String name, String mobile,
			String gender, String dob) {
		Boolean g = Boolean.parseBoolean(gender);
		try {
			UserProfile profile = new UserProfile(name, uId, g);
			profile.setDateOfBirth(new Date(Long.parseLong(dob)));
			profile.setMobileNumber(Integer.parseInt(mobile));
			pm.makePersistent(profile);
			System.out.println("oki");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public UserProfile getUserProfile(String userId) {
		javax.jdo.Query query = pm.newQuery(UserProfile.class);
		query.setFilter("uId == userIdentification");
		query.declareParameters("java.lang.String userIdentification");

		try {
			List<UserProfile> results = (List<UserProfile>) query.execute(userId);
			if(results.size() > 1 || results.size()==0 || results == null) {
				System.out.println("ERROR in getUserProfile function");
			}
			return results.get(0);
		} catch (Exception e) {
			e.printStackTrace();
			//query.closeAll();
		}
		UserProfile temp = null;
		return temp;
	}
	
	public UserPreferences getUserPreferences(String userId) {
		javax.jdo.Query query = pm.newQuery(UserPreferences.class);
		query.setFilter("uId == userIdentification");
		query.declareParameters("java.lang.String userIdentification");

		try {
			List<UserPreferences> results = (List<UserPreferences>) query.execute(userId);
			System.out.println(results.size());
			if(results.size() > 1 || results.size()==0 || results == null) {
				System.out.println("ERROR in getUserPreferences function");
			}
			return results.get(0);
		} catch (Exception e) {
			e.printStackTrace();
			//query.closeAll();
		}
		UserPreferences temp = null;
		return temp;
	}
}
